package com.papper.mng.controller.sys;

import com.papper.common.bean.sys.SysCode;
import com.papper.common.bean.sys.SysConfig;
import com.papper.common.config.LocalConfig;
import com.papper.common.config.SysCodeHelp;
import com.papper.common.config.SysConfigHelp;
import com.papper.common.enums.DataStatusEnum;
import com.papper.common.exception.ServiceException;
import com.papper.common.service.sys.SysConfigService;
import com.papper.common.utils.tool.StringTool;
import com.papper.mng.controller.BaseAction;
import com.papper.mng.domain.DwzResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/sys/config")
public class SysConfigAction extends BaseAction {
	@Autowired
	SysConfigService sysConfigService;

	/**
	 * 系统配置
	 *
	 * @return
	 */
	@GetMapping(value = "")
	public String execute(Model model, Long pid, String sv) {
		// 顶级字典
		Example          example  = new Example(SysCode.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("parentId", 0);
		if (!StringTool.isEmpty(sv)) {
			criteria.andLike("codeKey", "%" + StringTool.nullToEmpty(sv) + "%");
		}
		example.setOrderByClause("code_sort asc");
		List<SysConfig> codes  = sysConfigService.find(example);
		SysConfig       parent = sysConfigService.findById(pid);
		if (parent == null && codes == null) {
			return render("sys/config/index");
		}
		if (parent == null && codes.size() > 0) {
			parent = codes.get(0);
		} else if (parent != null && parent.getParentId().intValue() != 0) {
			parent = codes.get(0);
		}
		if (parent == null || codes == null) {
			return render("sys/config/index");
		}
		model.addAttribute("codes", codes);
		model.addAttribute("parent", parent);
		model.addAttribute("pid", parent.getId());
		model.addAttribute("sv", StringTool.nullToEmpty(sv));
		// 字典配置信息
		List<SysConfig> childCodes = sysConfigService.findByParentId(parent.getId());
		model.addAttribute("childCodes", childCodes);
		return render("sys/config/index");
	}

	/**
	 * 系统配置
	 *
	 * @return
	 */
	@RequestMapping(value = "/child")
	public String child(Model model, Long pid) {
		SysConfig parent = sysConfigService.findById(pid);
		model.addAttribute("parent", parent);
		List<SysConfig> codes = sysConfigService.findByParentId(pid);
		model.addAttribute("codes", codes);
		return render("sys/config/child");
	}

	/**
	 * 系统配置_新增
	 *
	 * @param pid
	 * @
	 */
	@GetMapping(value = "/add")
	public String add_get(Model model, Long pid) {
		model.addAttribute("nick", getNick());
		if (pid == 0) {
			model.addAttribute("pid", pid);
			model.addAttribute("pname", "顶级配置");
		} else {
			SysConfig parent = sysConfigService.findById(pid);
			model.addAttribute("pid", pid);
			model.addAttribute("pname", parent.getCodeKey());
		}
		return render("sys/config/add");
	}

	@PostMapping(value = "/add")
	@ResponseBody
	public DwzResponse add_post(HttpServletRequest _request, SysConfig SysConfig) {
		try {
			checkSuperCert(_request);
		} catch (ServiceException e) {
			return new DwzResponse("300", e.getMessage());
		}
		if (SysConfig == null) {
			return new DwzResponse("300", "参数[SysConfig]异常！");
		}
		SysConfig dbCode = sysConfigService.findByKey(SysConfig.getCodeKey(), SysConfig.getParentId().longValue());
		if (dbCode != null) {
			return new DwzResponse("300", "该值已存在！");
		}
		try {
			SysConfig.setDataStatus(DataStatusEnum.OPEN.getCode());
			sysConfigService.save(SysConfig);
			return new DwzResponse("200", "系统配置添加成功!!!");
		} catch (Exception e) {
			e.printStackTrace();
			return new DwzResponse("300", "系统配置添加失败:" + e.getMessage() + "!!!");
		}
	}

	/**
	 * 系统配置_更新
	 *
	 * @param _response
	 * @param id
	 * @
	 */
	@GetMapping(value = "/edit")
	public String edti_get(HttpServletResponse _response, Model model, Long id) {
		SysConfig entity = sysConfigService.findById(id);
		model.addAttribute("entity", entity);
		model.addAttribute("nick", getNick());
		return render("sys/config/edit");
	}

	@PostMapping(value = "/edit")
	@ResponseBody
	public DwzResponse edit_post(HttpServletRequest _request, SysConfig SysConfig) {
		try {
			checkSuperCert(_request);
		} catch (ServiceException e) {
			return new DwzResponse("300", e.getMessage());
		}
		if (SysConfig == null || SysConfig.getId() == null) {
			return new DwzResponse("300", "参数[SysConfig]异常！");
		}
		try {
			sysConfigService.update(SysConfig);
			return new DwzResponse("200", "系统配置修改成功!!!");
		} catch (Exception e) {
			e.printStackTrace();
			return new DwzResponse("300", "系统配置修改失败:" + e.getMessage());
		}
	}

	/**
	 * 系统配置_删除
	 *
	 * @param id
	 * @
	 */
	@PostMapping(value = "/del")
	@ResponseBody
	public DwzResponse del_post(HttpServletRequest _request, Long id) {
		try {
			checkSuperCert(_request);
		} catch (ServiceException e) {
			return new DwzResponse("300", e.getMessage());
		}
		if (id == null || id.intValue() <= 0) {
			return new DwzResponse("300", "参数[id]异常！");
		}
		try {
			sysConfigService.deleteById(id);
			return new DwzResponse("200", "系统配置删除成功!!!");
		} catch (Exception e) {
			e.printStackTrace();
			return new DwzResponse("300", "系统配置修改失败:" + e.getMessage());
		}
	}

	/**
	 * 系统配置_详情
	 *
	 * @return
	 */
	@GetMapping(value = "/detail")
	public String detail(HttpServletResponse _response, Model model, Long id) {
		SysConfig sysConfig = sysConfigService.findById(id);
		model.addAttribute("entity", sysConfig);
		return render("sys/config/detail");
	}

	/**
	 * 系统配置_刷新缓存
	 */
	@PostMapping(value = "/flush")
	@ResponseBody
	public DwzResponse flush_post(HttpServletRequest _request) {
		try {
			checkSuperCert(_request);
		} catch (ServiceException e) {
			return new DwzResponse("300", e.getMessage());
		}
		SysConfigHelp.I().initPropertys();
		SysCodeHelp.I().initPropertys();
		LocalConfig.I().initPropertys();
		//TODO-关联刷新
		//                redisCacheMqService.producerEvent("MqConfigFlush", "1");
		return new DwzResponse("200", "系统配置刷新成功!!!");
	}
}
